using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace WillStott.MxAppToolbox
{
	/// <summary>
	/// DlgUtils contains a collection of methods that are useful when 
	/// implementing dialog boxes
	/// </summary>
	public class DlgUtils
	{
        /// <summary>
        /// Removes selected item from a list box and selects the next item
        /// </summary>
        /// <param name="box">List box</param>
        /// <param name="lastRemovedItem">Set to -1, only used during recursion</param>
        /// <returns>index of last item removed</returns>
        static public int RemoveSelectedItemFromListBox(ListBox box, int lastRemovedItem)
        {
            int rc = lastRemovedItem;
            for (int x = 0; x < box.Items.Count; x++)
            {
                if (box.GetSelected(x) == true)
                {
                    box.Items.RemoveAt(x);
                    rc = RemoveSelectedItemFromListBox(box, x);
                }
            }
            return rc;
        }

		/// <summary>
		/// Gets a lists of the all controls found in dlg, not just top level 
		/// but controls within controls
		/// </summary>
		/// <param name="dlg">Form containing controls</param>
		/// <returns>array of controls found in dlg</returns>
	    static public Control[] GetAllControls(Form dlg)
	    {
	    	ArrayList allControlList = new ArrayList();

			RecurseControls(allControlList, dlg.Controls);

			return allControlList.ToArray(typeof(Control)) as Control[];
	    }

		static private void RecurseControls(ArrayList list, Control.ControlCollection controls)
		{
			foreach (Control control in controls)
			{
				list.Add(control);
				if ((control.Controls != null) && (control.Controls.Count > 0))
					RecurseControls(list, control.Controls);
			}
		}
		/// <summary>
		/// Opens HTML Help at topic for dialog box and at bookmark associated with the 
		/// control that was active when user pressed F1 or the control the user clicked
		/// when mouse pointer is in help mode. Usually called from the dialog's HelpEventHandler
		/// <seealso cref="IMxDlgForm.HelpRequest"/>
		/// </summary>
		/// <param name="form">Dialog object</param>
		/// <param name="HelpTopic">Name of Help Topic</param>
		/// <param name="e">Event object</param>
		static public  void ShowHelp(Form form, string HelpTopic, HelpEventArgs e)
		{
			Control ctrl = form.ActiveControl;
			Control parent = form;
			if (Control.MouseButtons == MouseButtons.Left )
			{
				Point pt = form.PointToClient(e.MousePos);
				foreach (Control control in GetAllControls(form) )
				{
					if ( control.Bounds.Contains(pt))
					{
						ctrl = control;
						break;
					}
				}
			}
			if (ctrl != null )
			{
				if ((ctrl.AccessibleName != null) && (ctrl.AccessibleName.Length > 0))
					HelpTopic += @"#" + ctrl.AccessibleName;
				parent = ctrl.Parent;
			}
			if ((Program.Instance.HelpFilePathName != null) && (Program.Instance.HelpFilePathName.Length > 0))
				Help.ShowHelp(parent, Program.Instance.HelpFilePathName, HelpTopic); 
			e.Handled = true;
		}
	}
}
